#include "DXUT.h"
#include "Item.h"

Item::Item(DWORD id, DWORD quantity) {
	m_Quantity	= quantity;
	m_ID		= id;
}

Item::Item(Item * item) {
	m_Quantity	= item->m_Quantity;
	m_ID		= item->m_ID;
}
/*
Item * Item::clone() {
	return new Item(this);
}
*/
ItemBlock::ItemBlock(Block block, DWORD quantity) 
	: Item(block.blockID, quantity) {
	m_Block		= block;
}

ItemBlock::ItemBlock(ItemBlock * item) : Item(item) {
	m_Block		= item->m_Block;	
}

 Item * ItemBlock::clone() {
	return new ItemBlock(this);
}


#ifdef RENDER
Inventory::Inventory(BYTE width, BYTE size, HUDDialogResourceManager* manager) {
	/* DEBUG */
	width = 10;
	size = 10;
	/* DEBUG */

	m_Linewidth = width;
	m_Size		= size;
	m_Selected	= 0;
	m_Items		= (Item**) calloc(size, sizeof(Item*)); ZeroMemory(m_Items, size * sizeof(Item*));
	//m_HUDCoord	= (BYTE*) calloc(size, sizeof(BYTE));
	
	m_HUD		= new HUDDialog();
	m_HUD->Init(manager);
	m_HUD->SetLocation(300, 720);/*
	m_HUD->AddItem( 5, &m_ItemHUD);
	*/
	/* DEBUG */
	m_Items[0] = new ItemBlock(DIRT_BLOCK, 1);
	m_Items[1] = new ItemBlock(GRASS_BLOCK, 1);
	m_Items[2] = new ItemBlock(STONE_BLOCK, 1);
	m_Items[3] = new ItemBlock(TORCH, 1);
	m_Items[4] = new ItemBlock(REDSTONE_TORCH, 1);
	m_Items[5] = new ItemBlock(REDSTONE_DUST, 1);
	//m_Items[6] = new ItemBlock(LEAF_BLOCK, 1);
	m_Items[7] = new ItemBlock(GLOWSTONE_BLOCK, 1);
	m_Items[8] = new ItemBlock(STILLWATER_BLOCK, 1);
	m_Items[9] = new ItemBlock(STILLLAVA_BLOCK, 1);
	/*
	for (int i = 0; i < 10; i++)
		m_HUDCoord[0] = m_ItemHUD->addItem(i * 50, 0, m_Items[i]);
	/* DEBUG */
	
	m_HUD->AddSelected( 6, -25, -25, 50, 50, false, &m_SelectionHUD);
};
#else
Inventory::Inventory(BYTE width, BYTE size) {
	/* DEBUG */
	width = 10;
	size = 10;
	/* DEBUG */

	m_Linewidth = width;
	m_Size		= size;
	m_Selected	= 0;
	m_Items		= (Block*) calloc(size, sizeof(Block));
};
#endif


Inventory::~Inventory() {
	for (int i = 0; i < m_Size; i++)
		delete m_Items[i];
	free(m_Items);
#ifdef RENDER
//	free(m_HUDCoord);
	delete m_HUD;
#endif
};

#ifdef RENDER
void Inventory::OnRender( float fElapsedTime ) {
	m_HUD->OnRender( fElapsedTime );
};
#endif

void Inventory::setItem(Item * item, BYTE index) {
	if (index < m_Size) {
		m_Items[index] = item;
	//	int x = (index % m_Linewidth) * 50;
	//	int y = (index / m_Linewidth) * 50;
		#ifdef RENDER
	//	m_ItemHUD->setItem(m_HUDCoord[index], x, y, item);
		#endif
	}
};

void Inventory::removeItem( BYTE index ) {
	if (index < m_Size) {
		SAFE_DELETE(m_Items[index]);
	}
};

void Inventory::NullItem( BYTE index ) {
	if (index < m_Size) {
		m_Items[index] = NULL;
	}
};

Item* Inventory::getSelected() {
	return m_Items[m_Selected];
};

void Inventory::removeSelected() {
	SAFE_DELETE(m_Items[m_Selected]);
};

void Inventory::setSelected(int index) {
	m_Selected = min(max(index + m_Selected, 0), m_Size - 1);
	#ifdef RENDER
	m_SelectionHUD->SetLocation(-25 + m_Selected * 50, -25);
	#endif
};

Item* Inventory::getItemAtIndex(int index) {
	if (index < m_Size && index >= 0)
		return m_Items[index];
	else
		return NULL;
};